const userApi = require("./api/user.js");
const api = require('./utils/request.js')
App({
  async onLaunch() {
    wx.getSystemInfo({
      success: (res) => {
        const safeArea = res.safeArea;
        const screenHeight = res.screenHeight;
        const bottomPadding = screenHeight - safeArea.bottom;
        this.globalData.screenWidth = res.screenWidth;
        this.globalData.bottomPadding = bottomPadding;
      },
    });
    wx.login({
      success: async (res) => {
        if (res.code) {
          wx.request({
            url: `${api.BASE_URL}/wxUser/login`,
            data: { code: res.code },
            method: "GET",
            success: async (resp) => {
              wx.setStorageSync("loginInfo", resp.data.data);
              this.globalData.isLogin = true;
              let rp = await userApi.getUserInfo({
                userId: resp.data.data.userId
              })
              wx.setStorageSync('userInfo', rp.data)
            },
          });
        }
      },
    });
  },
  globalData: {
    isLogin: false,
    screenWidth: 0,
    bottomPadding: 0, //安卓和苹果底部高度不一样
    userInfo: null,
    jump: false, //用于标记进我的页面时，是点我的，还是从其它页面跳转来的
    listeners: [],
  },
  // 注册监听器函数的方法
  registerListener: function (listener) {
    this.globalData.listeners.push(listener);
  },
  // 触发监听器函数的方法
  triggerListeners: function () {
    let listeners = this.globalData.listeners;
    for (let i = 0; i < listeners.length; i++) {
      listeners[i]();
    }
  },
});
